<?php
namespace common\services;

use common\models\Region;
use yii\data\Pagination;
use yii\data\ActiveDataProvider;

class RegionServices extends Region
{

    /**
     * 地区表
     * @param $params
     * @return array
     */
    public function search($params){
        $model = Region::find(false);
        $model->orderBy('order desc,id asc');

        $order = [
            'order' => SORT_DESC,
            'id' => SORT_ASC
        ];
        //是否输出数组
        if (isset($params['is_array']) && $params['is_array']) {
            $model->asArray();
        }
        //查询指定数据
        if (!empty($params['field'])) {
            $model->select($params['field']);
        }
        //是否指定排序
        if (!empty($params['order'])) {
            $order = $params['order'];
        }
        $dataProvider = new ActiveDataProvider([
            'query' => $model,
            'pagination' => [
                'pagesize' => $params['defaultPageSize'],
            ],
            'sort' => [
                'defaultOrder' => $order
            ]
        ]);

        if (isset($params['status']) && $params['status'] != '') {
            $model->andFilterWhere(['=', 'status', $params['status']]);
        }

        if (isset($params['is_index']) && $params['is_index'] != '') {
            $model->andFilterWhere(['=', 'is_index', $params['is_index']]);
        }

        if (isset($params['is_hot']) && $params['is_hot'] != '') {
            $model->andFilterWhere(['=', 'is_hot', $params['is_hot']]);
        }

        if (!empty($params['keywords'])){
            $model->andFilterWhere(['or',
                ['like', 'name', $params['keywords']],
                ['like', 'name_pinyin', $params['keywords']],
                ['like', 'code', $params['keywords']],
                ['like', 'path', $params['keywords']]
            ]);
        }

        return $dataProvider;
    }

    /**
     * 获取地区列表
     * @param int $pid
     * @return $this
     */
    public function getRegion($pid = 0){
        $model = Region::find(false)
            ->andFilterWhere(['=', 'status', Region::STATUS_FLAG_NORMAL])
            ->andFilterWhere(['=', 'pid', $pid])
            ->select('id,name')
            ->orderBy('order desc,id asc')
            ->asArray()
            ->all();

        return $model;
    }

    /**
     * 热门城市
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getRegionHot() {
        $model = Region::find(false);
        $res = $model->andFilterWhere(['=', 'status', Region::STATUS_FLAG_NORMAL])
            ->andFilterWhere(['=', 'is_hot', 1])
            ->select('id,name,name_pinyin')
            ->orderBy('order desc,id asc')
            ->asArray()
            ->all();

        return $res;
    }

    /**
     * 根据地区拼音首字母排序
     * @param bool $withCache
     * @return array|mixed
     */
    public function getRegionByPinyin($withCache = true){
        $cacheKey = 'Region_pinyin_group';

        $result = [];
        if ($withCache) {
            $result = \Yii::$app->cache->get($cacheKey);
        }

        if (empty($result)) {
            $model = Region::find(false);
            $res = $model->andFilterWhere(['=', 'status', Region::STATUS_FLAG_NORMAL])
                ->andFilterWhere(['=', 'pid', 0])
                ->select('id,name,name_pinyin')
                ->orderBy('order desc,id asc')
                ->asArray()
                ->all();

            if (!empty($res)) {
                foreach ($res as $val) {
                    $first = strtoupper(substr($val['name_pinyin'], 0, 1));
                    if (in_array($first, array('A', 'B', 'C', 'D', 'E', 'F'))) {
                        $result['A'][] = $val;
                    } elseif (in_array($first, array('G', 'H', 'J', 'K', 'L', 'M'))) {
                        $result['B'][] = $val;
                    } elseif (in_array($first, array('N', 'O', 'P', 'Q', 'R', 'S'))) {
                        $result['C'][] = $val;
                    } elseif (in_array($first, array('T', 'U', 'W', 'X', 'Y', 'Z'))) {
                        $result['D'][] = $val;
                    } else {
                        $result['E'][] = $val;
                    }
                }
            }

            if ($withCache && !empty($result)) \Yii::$app->cache->set($cacheKey, $result);

        }

        return $result;

    }

}
